package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysLog;
import com.skivingcloud.admin.sys.mapper.SysLogMapper;
import com.skivingcloud.admin.sys.service.ISysLogService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-28
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

	@Autowired
	SysLogMapper sysLogMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
    public void insertLog(SysLog sysLog) {
		sysLog.setId(UuidUtil.get32UUID());
		sysLogMapper.insert(sysLog);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		Map<String, Object> paramsNew = new HashMap<String, Object>();
		if (params.get("staff_name") != null && !"".equals(params.get("staff_name"))) {
			paramsNew.put("staff_name", "%" + params.get("staff_name").toString() + "%");
		}

		if (params.get("username") != null && !"".equals(params.get("username"))) {
			paramsNew.put("username", "%" + params.get("username").toString() + "%");
		}

		if (params.get("user_ip") != null && !"".equals(params.get("user_ip"))) {
			paramsNew.put("user_ip", "%" + params.get("user_ip").toString() + "%");
		}

		if (params.get("oper_time_start") != null && !"".equals(params.get("oper_time_start"))) {
			paramsNew.put("oper_time_start", params.get("oper_time_start").toString() + " 00:00:00");
		}

		if (params.get("oper_time_end") != null && !"".equals(params.get("oper_time_end"))) {
			paramsNew.put("oper_time_end", params.get("oper_time_end").toString() + " 23:59:59");
		}
		List<Map<String, Object>> list = sysLogMapper.selectByParams(paramsNew);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

}
